package com.taptech.doufu.ui.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import com.klinker.android.link_builder.Link;
import com.klinker.android.link_builder.LinkBuilder;
import com.taptech.doufu.R;
import com.taptech.doufu.ui.activity.NewOthersPersonalActivity;
import com.taptech.doufu.constant.Constant;
import com.taptech.doufu.bean.ListBean;
import com.taptech.doufu.ui.activity.UserDetailActivity;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by zhangyongcun on 16/8/10.
 */
public class itemSpeakerAdapter extends ArrayAdapter<ListBean>{

    private Context mContext;
    private int mLayoutResId;
    private List<ListBean> lists;

    public itemSpeakerAdapter(Context context, int resource, List<ListBean> list) {
        super(context, resource, list);
        this.mContext = context;
        this.mLayoutResId = resource;
        this.lists = list;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        convertView = null;
        if (convertView == null){
            LayoutInflater inflater = ((Activity)mContext).getLayoutInflater();
            convertView = inflater.inflate(mLayoutResId,parent,false);
            viewHolder = new ViewHolder();
            viewHolder.item_info = (TextView) convertView.findViewById(R.id.speaker_wall_item_txt);
            viewHolder.item_info.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    return true;
                }
            });
            convertView.setTag(viewHolder);
        }else {
            viewHolder = (ViewHolder) convertView.getTag();
        }


        int splitSize = lists.get(position).getMsg().size();
        List<Link> links = new ArrayList<>();
        for (int i = 0; i < splitSize; i++) {
            ListBean.MsgBean msgBean = lists.get(position).getMsg().get(i);
            viewHolder.item_info.append(msgBean.getSplit_text());
            if (msgBean.getUid() != null) {
                links.add(setLink(msgBean.getUid(),msgBean.getSplit_text(),msgBean.getDay_color()));
            }
        }

        if (links.size() > 0) {
            LinkBuilder.on(viewHolder.item_info).addLinks(links).build();
        }
        return convertView;
    }

    private Link setLink(final String uuid, final String userName, String color) {
        Link link = new Link(userName);
        link.setUnderlined(false);
        link.setTextColor(Color.parseColor("#" + color));
        link.setOnClickListener(new Link.OnClickListener() {
            @Override
            public void onClick(String clickedText) {
                startLink(uuid);
            }
        });
        return link;
    }


    private void startLink(String uuid) {
        UserDetailActivity.Companion.startActivity(mContext,uuid);
    }

    static class ViewHolder{
        TextView item_info;
    }
}
